﻿namespace Creek.UI.EFML.Base.JS.ScriptingTypes
{
    using System;
    using System.Reflection;
    using System.Windows.Forms;

    using Creek.UI.EFML.Base.Controls;

    public class Window
    {
        #region Fields

        public Get get;

        private readonly Form f;

        #endregion

        #region Constructors and Destructors

        public Window(Form f)
        {
            this.f = f;
            this.get = new Get(Environment.GetCommandLineArgs()[0]);
        }

        public IStyle style
        {
            get
            {
                return new ControlStyle(this.f);
            }
        }

        public string title
        {
            get
            {
                return this.f.Text;
            }
            set
            {
                this.f.Text = value;
            }
        }

        #endregion

        #region Public Properties

        public bool isXMLHttpRequest
        {
            get
            {
                return true;
            }
        }

        #endregion

        #region Public Methods and Operators

        public void AddEventHandler(string name, dynamic handler)
        {
            EventInfo evt = this.f.GetType().GetEvent(name);
            var action = new Action<object, object>((sender, args) => handler(sender, args));
            evt.AddEventHandler(this.f, Delegate.CreateDelegate(evt.EventHandlerType, action.Target, action.Method));
        }

        #endregion
    }
}